const query = require('../../utils/query')

// 获取标签名称数据
exports.getTagName = async () => {
  let sql = `select tag_id, tag_name from tag`
  let result = await query(sql)
  return result
}

// 修改标签
exports.editTag = async (tagData) => {
  let { tag_id, tag_name, is_show } = tagData
  let sql = `update tag set tag_name = '${tag_name}', is_show = ${is_show} where tag_id = ${tag_id}`
  let result = await query(sql)
  return result
}

// 获取编辑标签的数据
exports.editedTag = async (tag_id) => {
  let sql = `select * from tag where tag_id = ${tag_id}`
  let result = await query(sql)
  return result[0]
}

// 删除选中标签
exports.delSelTag = async (tag_ids) => {
  let sql = `delete from tag where tag_id in (${tag_ids})`
  let result = await query(sql)
  return result
}

// 删除标签
exports.delTag = async (tag_id) => {
  let sql = `delete from tag where tag_id = ${tag_id}`
  let result = await query(sql)
  return result
}

// 校验标签名是否重复
exports.verifyName = async (tag_id, tag_Name) => {
  let sql = `select count(tag_name) as count from tag where tag_name = '${tag_Name}' and tag_id != ${tag_id}`
  let result = await query(sql)
  return result[0].count
}

// 添加标签
exports.addTag = async (tagData) => {
  let { tag_name, is_show } = tagData
  let sql = `insert into tag(tag_name, is_show) values ('${tag_name}', ${is_show})`
  let result = await query(sql)
  return result
}

// 获取标签数量
exports.getCount = async (tag_name, is_show) => {
  let sql = `select count(tag_id) as tagCount from tag where 1 `
  if (tag_name) {
    sql += ` and tag_name like '%${tag_name}%'`
  }
  if (is_show) {
    sql += ` and is_show = ${is_show}`
  }
  let result = await query(sql)
  return result[0].tagCount
}

// 获取标签数据
exports.getTag = async (page, pagesize, tag_name, is_show) => {
  let offset = (page - 1) * pagesize

  let sql = `select * from tag where 1 `
  if (tag_name) {
    sql += ` and tag_name like '%${tag_name}%'`
  }
  if (is_show) {
    sql += ` and is_show = ${is_show}`
  }
  sql += ` limit ${offset}, ${pagesize}`
  let result = await query(sql)
  return result
}
